//
//  MRDiscount_ViewController.m
//  Travel_World
//
//  Created by dllo on 15/10/15.
//  Copyright © 2015年 TLS. All rights reserved.
//

#import "MRDiscount_ViewController.h"
#import "MRCountry_DiscountTableViewCell.h"
#import "MRCountry_DiscountModel.h"
#import "QTAFNnetWorkTool.h"
#import "UIImageView+WebCache.h"
#import "MRCountry_messageViewController.h"

@interface MRDiscount_ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *array;

@end

@implementation MRDiscount_ViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.title = [NSString stringWithFormat:@"%@特价折扣",self.content];
    
    self.navigationController.navigationBar.translucent = NO;
    
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createTableView];
    
    [self createData];
}

#pragma mark 数据解析
- (void)createData
{
    [QTAFNnetWorkTool getUrl:self.url body:nil hudSuperView:self.view result:QTJSON headerFile:nil success:^(id result) {
       
        self.array = [MRCountry_DiscountModel baseModelByArr:result[@"data"][@"list"]];
        
        [self.tableView reloadData];
        
    }];
}

#pragma mark 创建tableView
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.rowHeight = 90;
}

#pragma mark tableView的cell的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

#pragma mark 创建tableView的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    MRCountry_DiscountTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (self.array.count > indexPath.row) {
        if (!cell) {
            
            cell = [[MRCountry_DiscountTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
            
        }
        
        MRCountry_DiscountModel *model = self.array[indexPath.row];
        
        [cell.ImageView sd_setImageWithURL:[NSURL URLWithString:model.photo]placeholderImage:[UIImage imageNamed:@"placeholder"]];
        
        cell.title.text = model.title;
        
        NSString *priceStr = model.price;
        NSString *price = [priceStr substringWithRange:NSMakeRange(4, priceStr.length-11)];
        cell.price.text = [NSString stringWithFormat:@"%@元起",price];
        
        cell.discount.text = model.priceoff;
//        cell.price.text = [NSString stringWithFormat:@"%@元起",model.price];
        
        return cell;
    } else {
        
        return nil;
        
    }
}

#pragma mark tableView的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.array.count > indexPath.row) {
        
        MRCountry_messageViewController *messVC = [[MRCountry_messageViewController alloc] init];
        
        messVC.url = [NSString stringWithFormat:@"http://m.qyer.com/z/deal/%@/?source=app&client_id=qyer_ios&track_app_version=6.7&track_deviceid=41AE760B-510C-4778-AF4D-177B387E0F3B&track_user_id=6852801",[self.array[indexPath.row] Id]];
        
        [self.navigationController pushViewController:messVC animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
